<?php

namespace App\Models;

class SignUps extends Base
{
    protected $table = 'sign_ups';

    const STATUS_1 = 1;
    const STATUS_2 = 2;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">锁定</span>',
    ];

    const CHARGE_1 = 1;
    const CHARGE_2 = 2;

    const CHARGE_TEXT = [
        self::CHARGE_1 => '<span class="el-tag  el-tag--danger">收费</span>',
        self::CHARGE_2 => '<span class="el-tag">免费</span>',
    ];

    const PAY_STATE_1 = 1;
    const PAY_STATE_2 = 2;

    const PAY_STATE_TEXT = [
        self::PAY_STATE_1 => '<span class="el-tag  el-tag--danger">未支付</span>',
        self::PAY_STATE_2 => '<span class="el-tag">已支付</span>',
    ];

    protected $fillable = [
        'id','pay_sn','activity_id','activity_title','user_id','user_name','sex','phone','email','company','department','position','type','industry',
        'qq','size','hr_size','number','money','price','pay_sn','charge','pay_state','pay_at','status','created_at','updated_at'
    ];

    public function activity(){
        return $this->belongsTo('App\Models\Activitys', 'activity_id', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query
                        ->orwhere('user_name','like','%' . $params['kwd'] . '%')
                        ->orwhere('company','like','%' . $params['kwd'] . '%')
                        ->orwhere('activity_title','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['activity_id']) && $params['activity_id']) {
                    $query ->where('activity_id',$params['activity_id']);
                }
                if(isset($params['charge']) && $params['charge']) {
                    $query ->where('charge',$params['charge']);
                }
                if(isset($params['pay_state']) && $params['pay_state']) {
                    $query ->where('pay_state',$params['pay_state']);
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->where('status','<>',-1)
            ->with(['activity' => function ($query) {
                $query->select(['id','title'])->get();
            }])->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('created_at',$params['sort']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
            'charge'   => self::CHARGE_TEXT,
            'pay_state'   => self::PAY_STATE_TEXT,
        ]);
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = null, $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        if(isset($page)){
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['activity_id']) && $params['activity_id']) {
                        $query ->where('activity_id',$params['activity_id']);
                    }
                })->where('status','<>',-1)
                ->with(['activity' => function ($query) {
                    $query->select(['id','title','sponsor','start_at','enroll_at','status'])->get();
                }])->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else{
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['activity_id']) && $params['activity_id']) {
                        $query ->where('activity_id',$params['activity_id']);
                    }
                })->where('status','<>',-1)
                ->with(['activity' => function ($query) {
                    $query->select(['id','title','sponsor','start_at','enroll_at','status'])->get();
                }])->orderBy($sort, $orderby)->get();
        }
        return $lists;
    }

    /**
     * 根据id查询一条供应商
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function details($id, $fields = ['*']){
        return $this->select($fields)->where($id)
            ->with(['activity' => function ($query) {
                $query->select(['id','title','sponsor'])->get();
            }])->where('status','<>',-1)->first();
    }
    /**
     * 获取导出数据
     * @param array $params 条件参数
     * @return mixed
     */
    protected function exports($params){
        $fields = [
            'pay_sn','activity_title','user_name','sex','phone','email','qq','company','price','number',
            'money','charge','created_at','pay_state','pay_at','pay_way',
        ];
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params) && $params) {
                    $ids = explode('-',$params);
                    $query ->whereIn('id',$ids);
                }
            })->where('status','<>',-1)->get();

        foreach ($lists as $k=>$v){
            if($v->attributes['sex'] == 1){
                $v->attributes['sex'] = '男';
            }else{
                $v->attributes['sex'] = '女';
            }
            if($v->attributes['charge'] == 1){
                $v->attributes['charge'] = '收费';
            }else{
                $v->attributes['charge'] = '免费';
            }
            if($v->attributes['pay_state'] == 1){
                $v->attributes['pay_state'] = '未支付';
            }else{
                $v->attributes['pay_state'] = '已支付';
            }
            if($v->attributes['pay_way'] == 1){
                $v->attributes['pay_way'] = '免费';
            }elseif ($v->attributes['pay_way'] == 2){
                $v->attributes['pay_way'] = '微信';
            }else{
                $v->attributes['pay_way'] = '支付宝';
            }
        }
        return $lists;
    }

}
